import EmbarkJS from 'Embark/EmbarkJS';
import {{contractName}} from '{{relativeGenerationDir}}/contracts/{{contractName}}';

import React, { Component, Fragment } from 'react';
import ReactDOM from 'react-dom';
import { FormGroup, ControlLabel, FormControl, Checkbox, Button, Alert, InputGroup } from 'react-bootstrap';

function isSuccess(status) {
    return status === "0x1" || status === true;
}

{{#each functions}}
class {{capitalize name}}Form{{@index}} extends Component {
    constructor(props){
        super(props);
        this.state = {
            {{#if inputs.length}}
            input: {
                {{#each inputs}}
                {{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}}
                {{/each}}
            },
            {{/if}}
            {{#if payable}}
            value: 0,
            {{/if}}
            {{#ifview stateMutability}}
            output: null,
            {{/ifview}}
            error: null,
            mined: null
        };
    }

    handleChangeFile(e) {
        const {input} = this.state;
        input.file = [e.target];
        this.setState({input});
    }

    handleChange(e, name) {
        const {input} = this.state;
        input[name] = e.target.value;
        this.setState({input});
    }

    handleCheckbox(e, name) {
        const {input} = this.state;
        input[name] = e.target.checked;
        this.setState({input});
    }

    async handleClick(e){
        e.preventDefault();

        const {input, value} = this.state;

        this.setState({output: null, error: null, receipt: null});

        try {
        {{#if isStandard}}
        {{#ifview stateMutability}}
            const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call();
            {{#iflengthgt outputs 1}}
            this.setState({output: {
            {{#each outputs}}
                {{emptyname name @index}}: result[{{@index}}]{{#unless @last}},{{/unless}}
            {{/each}}
            }});
            {{else}}
            this.setState({output: result});
            {{/iflengthgt}}
        {{else}}
            {{#each inputs}}
            {{#ifarr type}}
            input.{{trim name}} = input.{{trim name}}.split(',').map(x => x.toString().trim());
            {{/ifarr}}
            {{/each}}

            const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}});

            const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});

            const receipt = await toSend.send({
                {{#if payable}}
                value: value,
                {{/if}}
                from: web3.eth.defaultAccount,
                gasLimit: estimatedGas
            });

            console.log(receipt);

            this.setState({receipt});
        {{/ifview}}
        {{/if}}

        {{#if isIpfsText}}
        {{#ifview stateMutability}}
            const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call();
            const data = await EmbarkJS.Storage.get(result);
            this.setState({output: data});
        {{else}}
            const hash = await EmbarkJS.Storage.saveText(input.{{inputs.1.name}});
            const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}(input.{{inputs.0.name}}, hash);

            const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});

            const receipt = await toSend.send({
                from: web3.eth.defaultAccount,
                gasLimit: estimatedGas
            });

            console.log(receipt);

            this.setState({receipt});
        {{/ifview}}
        {{/if}}

        {{#if isIpfsFile}}
        {{#ifview stateMutability}}
            const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call();
            const url = EmbarkJS.Storage.getUrl(result);
            this.setState({output: url});
        {{else}}
            const hash = await EmbarkJS.Storage.uploadFile(input.file)
            const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}(input._id, hash);

            const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});

            const receipt = await toSend.send({
                from: web3.eth.defaultAccount,
                gasLimit: estimatedGas
            });

            console.log(receipt);

            this.setState({receipt});
        {{/ifview}}
        {{/if}}
        } catch(err) {
            console.error(err);
            this.setState({error: err.message});
        }
    }

    render(){
        const {input, value, error, output, receipt} = this.state;

        return <div className="formSection">
            <h3>{{name}}</h3>
            <form>
            {{#if inputs.length}}
            {{#if isIpfsFile}}
            {{#ifview stateMutability}}
                <FormGroup>
                    <ControlLabel>_id</ControlLabel>
                    <FormControl
                        type="text"
                        placeholder="uint256"
                        onChange={(e) => this.handleChange(e, '_id')}
                    />
                </FormGroup>
            {{else}}
                <FormGroup>
                    <ControlLabel>_id</ControlLabel>
                    <FormControl
                        type="text"
                        placeholder="uint256"
                        onChange={(e) => this.handleChange(e, '_id')}
                    />
                </FormGroup>
                <FormGroup>
                    <ControlLabel>File</ControlLabel>
                    <FormControl
                        type="file"
                        onChange={(e) => this.handleChangeFile(e)}
                    />
                </FormGroup>
            {{/ifview}}
            {{else}}
                {{#each inputs}}
                <FormGroup>
                    <ControlLabel>{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}</ControlLabel>
                    {{#ifeq type 'bool'}}
                    <Checkbox
                        onClick={(e) => this.handleCheckbox(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')}
                    />
                    {{else}}
                    <FormControl
                        type="text"
                        defaultValue={ input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}} }
                        placeholder="{{type}}"
                        onChange={(e) => this.handleChange(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')}
                    />
                    {{/ifeq}}
                </FormGroup>
                {{/each}}
            {{/if}}
            {{/if}}
            {{#if payable}}
                <FormGroup>
                    <ControlLabel>Value</ControlLabel>
                    <InputGroup>
                        <InputGroup.Addon>Ξ</InputGroup.Addon>
                        <FormControl
                            type="text"
                            defaultValue={ value }
                            placeholder="{{type}}"
                            onChange={(e) => { this.setState({value: e.target.value}); }}
                        />
                        <InputGroup.Addon>wei</InputGroup.Addon>
                    </InputGroup>
                </FormGroup>
            {{/if}}

                { error != null && <Alert bsStyle="danger">{error}</Alert> }

            {{#ifview stateMutability}}
                <Button type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Call</Button>
                {
                    output &&
                    <Fragment>
                        <h4>Results</h4>
                        {{#iflengthgt outputs 1}}
                        <ul>
                        {{#each outputs}}
                            <li>{{emptyname name @index}}: { JSON.stringify(output.{{emptyname name @index}}) }</li>
                        {{/each}}
                        </ul>
                        {{else}}
                        {output.toString()}
                        {{/iflengthgt}}
                    </Fragment>
                }
            {{else}}
                <Button type="submit" bsStyle="primary" onClick={(e) => this.handleClick(e)}>Send</Button>
                {
                receipt &&
                <Fragment>
                    <Alert bsStyle={isSuccess(receipt.status) ? 'success' : 'danger'}>{isSuccess(receipt.status) ? 'Success' : 'Failure / Revert'} - Transaction Hash: {receipt.transactionHash}</Alert>
                </Fragment>

                }
            {{/ifview}}
            </form>
        </div>;
    }
}

{{/each}}

function {{contractName}}UI(props) {
    return (<div>
            <h1>{{contractName}}</h1>
            {{#each functions}}
            <{{capitalize name}}Form{{@index}} />
            {{/each}}
        </div>);
}


ReactDOM.render(<{{contractName}}UI />, document.getElementById('app'));
